<?php
namespace app\houtai\controller\system\rbac;
use app\houtai\controller\BaseController;
use think\db\Expression;

class NodeController extends BaseController {
  /**
   *  @brief  : 后台角色Action查询
   *  @return : 
   *  @detail : 
   */
  public function indexAction() {
    $ADMIN_ROLES = GetAdminRole();
    ////////////////////////////////////////////////////////////////////////////
    if (empty($ADMIN_ROLES)) {
      return abort(1, "系统无预定义角色!");
    }
    
    $RoleID = $this->request->param('RoleID/d', 0);
    if ($RoleID > 0) {
      if (!array_key_exists($RoleID, $ADMIN_ROLES)) {
        return abort(1, "角色[RoleID={$RoleID}]不存在");
      }
    } else {
      $RoleID = array_keys($ADMIN_ROLES);
      $RoleID = array_shift($RoleID);
    }

    // 角色所属菜单
    $menu = table('Ky_AdminAction a', 'rydb.websms')->field([
      'a.NavId', 'a.ParentId', 'a.NavTitle', 'a.LinkUrl', 'a.Remark', 'a.SortID',
      'ISNULL(b.ID, 0)' => 'ID', 'ISNULL(b.State, 0)' => 'State', 'ISNULL(b.RoleID, 0)' => 'RoleID',
    ])
    ->join('Ky_AdminRoleAction b', "a.NavId = b.NodeID AND b.RoleID = {$RoleID}", 'LEFT')
    ->where(['a.IsDelete' => 0, 'a.ParentId' => ['EGT', 0]])
    ->order('ParentId,SortID,NavTitle')
    ->select();
    $menu = list_to_tree($menu, 'NavId', 'ParentId');
    //echo table('Ky_AdminAction a', 'rydb.websms')->getLastSql();die;
    // 角色所属请求
    $rows = table('Ky_AdminAction a', 'rydb.websms')->field([
      'a.NavId', 'a.ParentId', 'a.NavTitle', 'a.LinkUrl', 'a.Remark', 'a.SortID',
      'ISNULL(b.ID, 0)' => 'ID', 'ISNULL(b.State, 0)' => 'State', 'ISNULL(b.RoleID, 0)' => 'RoleID',
      "UPPER ( REPLACE( LEFT ( LinkUrl, CHARINDEX('/', LinkUrl) - 1 ), '.', '_' ))" => 'GroupID',
    ])
    ->join('Ky_AdminRoleAction b', "a.NavId = b.NodeID AND b.RoleID = {$RoleID}", 'LEFT')
    ->where(['a.IsDelete' => 0, 'a.ParentId' => ['LT', 0], 'a.LinkUrl' => ['NEQ', '']])
    ->order('ParentId,SortID,GroupID,NavTitle')
    ->select();

    $list = [];
    foreach($rows as $row) {
      $GroupID = $row['GroupID'];
      if (!isset($list[$GroupID])) {
        $list[$GroupID] = [];
      }
      $list[$GroupID][] = $row;
    }

    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'ADMIN_ROLES' => $ADMIN_ROLES,
      'RoleID'  => $RoleID,
      'menu'    => $menu,
      'list'    => $list,
    ]);
  }
  /**
   *  @brief  : 保存勾选
   *  @return : JSON
   *  @detail :
   */
  public function saveAction() {
    if (!$this->request->isPost()) {
      return result([], 100, '非法请求');
    }
    $RoleID = $this->request->param('RoleID/d', 0);
    $Action = $this->request->param('Action/s', '');
    $Action = explode(';', $Action);
    if ($RoleID <= 0 || empty($Action)) {
      return result([], 101, '业务参数无效');
    }
    $Role = table('Ky_AdminRole', 'rydb.websms')->where(['ID' => $RoleID])->find();
    if (empty($Role)) {
      return result([], 102, "角色[RoleID={$RoleID}]不存在");
    }
    $ActionState = [];
    foreach($Action as $v) {
      $a = explode(':', $v);
      $b = $a[0] * 1;
      $c = $a[1] * 1;
      $c = $c > 0 ? 1 : 0;
      $ActionState[$b] = $c;
      $eax = table('Ky_AdminRoleAction', 'rydb.websms')->where(['RoleID' => $RoleID, 'NodeID' => $b])->count();
      if ($eax <= 0) {
        $eax = table('Ky_AdminRoleAction', 'rydb.websms')->insert(['RoleID' => $RoleID, 'NodeID' => $b, 'State' => $c]);
      } else {
        $eax = table('Ky_AdminRoleAction', 'rydb.websms')->where(['RoleID' => $RoleID, 'NodeID' => $b])->update(['State' => $c, 'UpdateTime' => new Expression('GETDATE()')]);
      }
    }
    $eax = table('Ky_AdminRoleAction', 'rydb.websms')->where(['RoleID' => $RoleID])->whereNotIn('NodeID', array_keys($ActionState))->delete();
    return result([], 0);
  }
}
